<template>
 <div class="p-2">
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleBatchDistribution" preIcon="ant-design:plus-outlined"> 批量分配检查人员</a-button>
        <a-button type="primary" @click="handleReset" preIcon="ant-design:plus-outlined"> 重置检查表</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)"/>
      </template>
      <!--字段回显插槽-->
      <template v-slot:bodyCell="{ column, record, index, text }">
      </template>
    </BasicTable>

    <TbBaseCheckRecordModal ref="registerModal" @success="handleSuccess"/>
    <SelectPersonnelModal ref="registerModalSelectPersonnel" @success="handleSuccess"/>
   </div>
</template>

<script lang="ts" setup>
  import { ref, reactive, unref, inject, watch } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage'
  import TbBaseCheckRecordModal from './components/TbBaseCheckRecordModal.vue'
  import SelectPersonnelModal from './components/SelectPersonnelModal.vue'
  import { tbBaseCheckRecordColumns } from './TbBaseCheckPlan.data';
  import { tbBaseCheckRecordList, tbBaseCheckRecordDelete, tbBaseCheckRecordDeleteBatch, tbBaseCheckRecordExportXlsUrl, tbBaseCheckRecordImportUrl } from './TbBaseCheckPlan.api';
  import { resetExaminer } from './TbBaseCheckRecord.api';
  import { isEmpty } from "/@/utils/is";
  import { useMessage } from '/@/hooks/web/useMessage';
  import { downloadFile } from '/@/utils/common/renderUtils';
  
  const toggleSearchStatus = ref<boolean>(false);
  //接收主表id
  const mainId = inject('mainId') || '';
  //提示弹窗
  const $message = useMessage()
  const queryParam = {};
  // 列表页面公共参数、方法
  const { prefixCls, tableContext, onImportXls, onExportXls } = useListPage({
    tableProps: {
      api: tbBaseCheckRecordList,
      columns: tbBaseCheckRecordColumns,
      canResize: false,
      useSearchForm: false,
      actionColumn: {
        width: 180,
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: '检查情况记录',
      url: tbBaseCheckRecordExportXlsUrl,
      params: {
        'planId': mainId
      }
    },
    importConfig: {
      url: ()=>{
        return tbBaseCheckRecordImportUrl + '/' + unref(mainId)
      }
    }
  });

  //注册table数据
  const [registerTable, { reload}, { rowSelection, selectedRowKeys }] = tableContext;
  const registerModal = ref();
  const formRef = ref();
  const labelCol = reactive({
    xs:24,
    sm:4,
    xl:6,
    xxl:4
  });
  const wrapperCol = reactive({
    xs: 24,
    sm: 20,
  });
  
  const registerModalSelectPersonnel = ref();
  
  /**
   * 新增事件
   */
  function handleAdd() {
    if (isEmpty(unref(mainId))) {
        $message.createMessage.warning('请选择一个主表信息')
        return;
    }
    registerModal.value.disableSubmit = false;
    registerModal.value.add();
  }

  /**
   * 编辑事件
   */
  async function handleEdit(record: Recordable) {
    selectedRowKeys.value.push(record.id)
    registerModalSelectPersonnel.value.disableSubmit = false;
    registerModalSelectPersonnel.value.edit(selectedRowKeys.value);
  }

  /**
   * 详情事件
   */
  function handleDetail(record: Recordable) {
    registerModal.value.disableSubmit = true;
    registerModal.value.edit(record);
  }
  
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await tbBaseCheckRecordDelete({id: record.id}, handleSuccess);
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await tbBaseCheckRecordDeleteBatch({ids: selectedRowKeys.value}, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '配置检查人',
        onClick: handleEdit.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ]
  }
  
  /**
   * 下拉操作栏
   */
  // function getDropDownAction(record){
  //   return [
  //     {
  //       label: '详情',
  //       onClick: handleDetail.bind(null, record),
  //     },
  //     {
  //       label: '删除',
  //       popConfirm: {
  //         title: '是否确认删除',
  //         confirm: handleDelete.bind(null, record),
  //       },
  //     },
  //   ];
  // }
  
  /**
   * 重置
   */
  function searchReset() {
    formRef.value.resetFields();
    selectedRowKeys.value = [];
    //刷新数据
    reload();
  }
  
  /**
   * 批量分配检查任人
   */
  async function handleBatchDistribution(record: Recordable) {
    console.log("----selectedRowKeys.value-------"+JSON.stringify(selectedRowKeys.value))
    if(selectedRowKeys.value.length >0){
      registerModalSelectPersonnel.value.disableSubmit = false;
      registerModalSelectPersonnel.value.edit(selectedRowKeys.value);
    }else{
      $message.createMessage.warning('请选中需要配置的检查项!')
    }
  }
  
  /**
   * 重置检查人
   */
  function handleReset(){
    if(selectedRowKeys.value.length >0){
      resetExaminer({ids: selectedRowKeys.value}, handleSuccess);
    }else{
      $message.createMessage.warning('请选中需要重置的检查项!')
    }
  }
  
  watch(mainId, () => {
    queryParam['planId'] = unref(mainId);
    reload();
  });
</script>
<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    padding: 0;
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }
    .query-group-cust{
      min-width: 100px !important;
    }
    .query-group-split-cust{
      width: 30px;
      display: inline-block;
      text-align: center
    }
  }
</style>
